package com.heartware.sh.blackhole.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.heartware.sh.blackhole.R;
import com.heartware.sh.blackhole.entity.test.MySay;

import java.util.List;

/**
 * Created by Administrator on 2016/12/7 0007.
 */
public class FiveListAdapter extends RecyclerView.Adapter<FiveListAdapter.MyViewHolder> {

    /**
     * 数据源...
     */
    private List<MySay> sayList;


    private MyItemClickListener itemClickListener;
    private MyItemLongClickListener itemLongClickListener;

    public void setSayList(List<MySay> sayList){
        this.sayList = sayList;
    }

    public void setItemClickListener(MyItemClickListener itemClickListener){
        this.itemClickListener = itemClickListener;
    }

    public void setItemLongClickListener(MyItemLongClickListener itemLongClickListener){
        this.itemLongClickListener = itemLongClickListener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.five_item, null);

        return new MyViewHolder(view, itemClickListener, itemLongClickListener);
    }

    @Override
    public int getItemCount() {
        return sayList.size();
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        MySay ms = sayList.get(position);

        holder.tv_content.setText(ms.getContent());
        holder.tv_author.setText(ms.getAuthor());

    }


    public interface MyItemClickListener{
        void onItemClick(View v, int position);
    }

    public interface MyItemLongClickListener{
        void onItemLongClick(View v, int position);
    }

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener{

        public TextView tv_content;
        public TextView tv_author;

        private MyItemClickListener itemClickListener;
        private MyItemLongClickListener itemLongClickListener;

        public MyViewHolder(View itemView, MyItemClickListener itemClickListener, MyItemLongClickListener itemLongClickListener) {
            super(itemView);
            tv_content = (TextView) itemView.findViewById(R.id.tv_content);
            tv_author = (TextView) itemView.findViewById(R.id.tv_author);

            this.itemClickListener = itemClickListener;
            this.itemLongClickListener = itemLongClickListener;

            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if(itemClickListener != null){
                itemClickListener.onItemClick(view, getPosition());
            }
        }

        @Override
        public boolean onLongClick(View view) {

            if(itemLongClickListener != null){
                itemLongClickListener.onItemLongClick(view, getPosition());
            }

            return true;
        }
    }

}
